package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.Goods;
/***
 * 物品信息
 * @author yy
 *
 */
@Service
public class GoodsDAO extends BaseDAO<Goods, Integer>{
	
	
	private final static String sql_goodsList = "FROM Goods WHERE oc = ?";
	public RESULT findByMap(List<Integer> rootCode, Map<String, String> map, PageBean pb){
		if(rootCode == null) return null;
		
		StringBuffer hql = new StringBuffer("FROM Goods g WHERE g.oc IN (");
		List<Object> list = new ArrayList<Object>();
		
		for (Integer ig : rootCode) {
			hql.append("?,");
			list.add(ig);
		}
		
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String name = map.get(Parm.NAME);
			String eName = map.get(Parm.ENAME);
			String oc = map.get(Parm.OC);
			String deleted = map.get(Parm.DELETED);
			String gtc = map.get(Parm.GTC);
			String sort = map.get(Parm.SORT);
			
			if(isNotNull(name)){
				hql.append(" AND g.name LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(eName)){
				hql.append(" AND g.eName LIKE ?");
				list.add("%" + eName + "%");
			}if(isNotNull(oc)){
				hql.append(" AND g.oc = ?");
				list.add(Integer.parseInt(oc));
			}if (isNotNull(deleted)) {
				hql.append(" AND g.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}else{
				hql.append(" AND g.deleted = 0");
			}
			if(isNotNull(gtc)){
				hql.append(" AND g.gtc = ?");
				list.add(Integer.parseInt(gtc));
			}if(isNotNull(sort)){
				hql.append(" AND g.sort = ?");
				list.add(Byte.parseByte(sort));
			}
		}
		hql.append(" ORDER BY g.code DESC");
		
		return findToPageBean(hql.toString(), pb, list);
	}
	public RESULT findByMapAndOcs(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if (null == ocs || ocs.size() == 0) return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer(" FROM Goods g WHERE g.deleted = 0 AND g.oc IN (");
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if (map != null) {
			String name = map.get(Parm.NAME);
			String eName = map.get(Parm.ENAME);
			String deleted = map.get(Parm.DELETED);
			String sort = map.get(Parm.SORT);
			String gtn = map.get(Parm.GTN);
			
			if(isNotNull(name)){
				hql.append(" AND g.name LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(eName)){
				hql.append(" AND g.eName LIKE ?");
				list.add("%" + eName + "%");
			}if (isNotNull(deleted)) {
				hql.append(" AND g.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}if (isNotNull(deleted)) {
				hql.append(" AND g.sort = ?");
				list.add(Byte.parseByte(sort));
			}if (isNotNull(gtn)) {
				hql.append(" AND g.gtn = ?");
				list.add("%" + gtn + "%");
			}
		}
		hql.append(" ORDER BY g.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	
	
	/**
	 * 根据机构编号在缓存中获取该机构的所有用户信息，以JSON方式返回
	 * @param	机构编号
	 */
	public String goodsList(Integer typeCode) {
		if (null == typeCode)
			return null;
		
		String result = Cache.goodsList.get(typeCode);
		if (!isNotNull(result))
			result = loadGoodsList(typeCode);
		return result;
	}
	
	/**
	 * 加载指定机构的所有用户信息到缓存，并以JSON方式保存
	 * @param	机构编号
	 */
	public String loadGoodsList(Integer typeCode) {
		List<Goods> list = find(sql_goodsList, typeCode);
		String result = null;
		if (null != list && list.size() > 0) {
			StringBuffer sb = new StringBuffer(Sys.LB);
			for (Goods g : list)
				sb.append("{\"name\":\"" + g.getName() + "\",\"code\":\"" + g.getCode()  + "\"},");
			
			sb.deleteCharAt(sb.length() - 1).append(Sys.RB);
			result = sb.toString();
			Cache.goodsList.put(typeCode, result);
			return result;
		}
		return Sys.B;
	}
	
	public RESULT update(Goods gt) {
		if (null == gt || gt.getCode() == null) return null;
		Goods db = this.get(gt.getCode());
		if (null == db) return RESULT.NOT_FIND;
		
		boolean ok = false;
		if (!isNotNull(db.getName()) && isNotNull(gt.getName()) || (!db.getName().equals(gt.getName()))) {
			db.setName(gt.getName());
			ok = true;
		}if (!isNotNull(db.getNorms()) && isNotNull(gt.getNorms()) || (!db.getNorms().equals(gt.getNorms()))) {
			db.setNorms(gt.getNorms());
			ok = true;
		}if (!isNotNull(db.getType()) && isNotNull(gt.getType()) || (!db.getType().equals(gt.getType()))) {
			db.setType(gt.getType());
			ok = true;
		}if ((db.getGtc() == null) && (gt.getGtc() != null) || (db.getGtc() != gt.getGtc())) {
			db.setGtc(gt.getGtc());
			ok = true;
		}if ((db.getSort() == null) && (gt.getSort() != null) || (db.getSort() != gt.getSort())) {
			db.setSort(gt.getSort());
			ok = true;
		}if (!isNotNull(db.getGtn()) && isNotNull(gt.getGtn()) || (!db.getGtn().equals(gt.getGtn()))) {
			db.setGtn(gt.getGtn());
			ok = true;
		}
		if (ok)
			return super.upd(db);
		
		return RESULT.SUCCESS;
	}
	
	
	public List<Goods> findByNameOc(String name, List<Integer> ocs){
		if (name == null || ocs == null) return null;
		StringBuffer hql = new StringBuffer("FROM Goods g WHERE g.name = ? AND g.oc IN ( ");
		List<Object> parm = new ArrayList<Object>();
		parm.add(name);
		for (Integer i : ocs) {
			if (null != i) {
				parm.add(i);
				hql.append("?,");
			}
		}	
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), parm.toArray());
	}
}
